<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        let calculate1 = str => {
            let len = str.length;
            for (var i = 0; i != len; i++) {
                if (len % i == 0) {
                    var j,
                        flag = 1;
                }
                for (var j = i; j < len; j++) {
                    if (str[j] == str[j % i]) {
                        continue;
                    } else {
                        flag = 0;
                        break;
                    }
                }
                if (flag) {
                    console.log(i);
                    break;
                }
            }

        }
        calculate1("ababab");
        calculate1("aaabbbaaabbbaaabbb");

        let splitArrayByLen = (str,len) => {
            var reg = new RegExp(".{"+ len +"}",'g');
            var strArr = str.match(reg);
            return strArr;
        }

        let calculate2 = str => {
            let len = str.length;
            var flag = 1;
            var newArr = [];
            for (var i = 2; i != len; i++) {
                if (len % i == 0) {
                    flag= 1;
                    newArr = splitArrayByLen(str, i);
                    for (var idx = 1, item = newArr.length; idx != item; idx ++) {
                        if (newArr[0] != newArr[idx]) {
                            flag = 0;
                            break;
                        }
                    }
                    if (flag == 1) {
                        console.log(i, newArr[0]);
                    }
                }
            }
        }
        calculate2("ababab");
        calculate2("aaabbbaaabbbaaabbb");
    </script>
</body>

</html>